<template>
  <div id="app">
    <el-card class="box-card">
      <el-form :model="ruleForm" :rules="rules" ref="validateForm">
        <el-form-item prop="iphone">
          <el-input placeholder="请输入手机号" v-model="ruleForm.iphone"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input placeholder="请输入密码" v-model="ruleForm.password"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button 
            type="primary" 
            style="width: 100%"
            @click="submitForm">
          >
            登录
          </el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
export default {
  name: 'App',
  data() {
    const validateIphone = (rule, value, callback) => {
      if (value.charAt(2) !== '9') {
        callback(new Error('手机号不是本地'))
      } else {
        callback()
      }
    }

    return {
      ruleForm: {
        iphone: '',
        password: ''
      },
      rules: {
        iphone: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { pattern: /^1[3-9]\d{9}$/, message: '请输入合法手机号', trigger: 'blur' },
          { validator: validateIphone, trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    submitForm () {
      this.$refs.validateForm.validate((valid) => {
        if (valid) {
          alert('submit!')
        } else {
          alert('error submit!!')
          return false;
        }
      })
    }
  }
}
</script>

<style>
body {
  padding: 0;
  margin: 0;
}
#app {
  display: flex;
  width: 100%;
  height: 100vh;
  background-color: pink;
  justify-content: center;
  align-items: center;
}
.box-card {
  width: 440px;
  height: 300px;
  box-sizing: border-box;
  padding-top: 50px;
}
</style>
